﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Application.Admin.Dto.AutoPushDto>
@{
    ViewBag.subtitle = " 定时推送";
    ViewBag.title = "定时推送";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{ Html.Telerik().Window()
        .Name("AddAutoPushWindow")
        .Title("添加定时推送")
        .Content(@<text>
    @Html.Partial("_AddAutoPush", new Application.Admin.Dto.AutoPushDto() { ExpiredDateTime = System.DateTime.Now.AddDays(2) })
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{ Html.Telerik().Window()
        .Name("AutoPushDetailWindow")
        .Title("编辑推送时间段")
        .Content(@<text>
    @Html.Partial("_EditAutoPushTimer")
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{ Html.Telerik().Window()
        .Name("NotePushWindow")
        .Title("定时推送注意事项")
        .Content(@<text>
    @Html.Partial("_Note")
    </text>)
        .Width(700)
        .Draggable(true).Modal(true).Visible(false)
        .Render();
}
@{Html.Telerik().Grid(Model)
    .Name("AutoPushGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "searchautopush"))
        {
        <label class="customer-label" for="Customers-input">
            定时推送名:
        </label>
        <input type="text" id="searchAutoPush" value="定时推送名" data-autocomplete="/push/searchautopush" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "AddAutoPush"))
        {
        <a id="btnAddAutoPush" href='javascript:;' class='t-button'>添加定时推送</a>  
        }
        if (Html.HasUiPermission(this.ViewContext, "MulDeleteAutoPush"))
        {
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>  
        }
    }
    <a id="notePush" href='javascript:;' class='t-button' style="float: right;">注意事项</a>
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();

        columns.Bound(o => o.Name).Width(150).Title("定时推送名");
        columns.Bound(o => o.Remark).Width(150).Title("定时推送描述");
        columns.Bound(o => o.Content).Width(300).Title("推送内容").EditorTemplateName("TextArea");
        columns.Bound(o => o.ExpiredDateTime).Format("{0:yyyy-MM-dd HH:mm:ss}").Width(150).Title("过期时间");
        columns.Bound(o => o.IntervalTime).Width(50).Title("时间间隔");
        columns.Bound(o => o.ContentId).Width(200).Title("推送内容Id").Hidden();

        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").Hidden();
        columns.Bound(o => o.AddTime).Width(200).Title("添加时间").Hidden();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();
        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "editautopush"))
            {
                comm.Edit().ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "autopushdetail"))
            {
                comm.Custom("customAutoPushTimer")
                    .Text("编辑时间段").Ajax(true)
                    .Action("autopushdetail", "push");
            }
            if (Html.HasUiPermission(this.ViewContext, "deleteautopush"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(300).Title("删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding").OnComplete("onComplete").OnRowDataBound("rowBound"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxautopushindex", "push")
        .Update("editautopush", "push")
        .Delete("deleteautopush", "push"))
    .Pageable(page => page.PageSize(15))
    .RowAction(row =>
    {
        if (row.DataItem.ExpiredDateTime < System.DateTime.Now)
        {
            row.HtmlAttributes["style"] = "color:red;";
        }
    })
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#AutoPushGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchAutoPush").val();
        if (searchKeyStr == '定时推送名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr });
    }

    function onComplete(e) {
        if (e.name == "customAutoPushTimer") {
            var detailWindow = $("#AutoPushDetailWindow").data("tWindow");
            var res = e.response;
            $('#divAutoPushDetail').html(res.toString());
            detailWindow.center().open();
        }
        else {
            $("input:checkbox[name='selectRow']").attr("checked", false);
            var success = e.response.success;
            if (success != null && success != undefined) {
                alert(e.response.message);
                window.location.reload();
            }
        }
    }

    //过期显示红色
    function rowBound(e) {
        var exp = e.dataItem.ExpiredDateTime;
        var now = new Date();
        if (now - exp >= 0) {
            var colStr = "";
            var columns = $(e.row.outerHTML).children();
            $.map(columns, function (item) {
                $(item).css('color', 'red');
                colStr += item.outerHTML;
            })
            e.row.innerHTML = colStr;
        }
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/push/muldeleteautopush', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#AutoPushGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function AddEditAutoPushDetail() {
        var url = '/push/editautopushdetail';
        var dataStr = $('#divAutoPushDetail input[type=checkbox]:checked , #autoPushId');
        jQuery.post(url, dataStr, function (res) {
            if (res.success) {
                alert('编辑成功');
            } else {
                alert(res.message);
            }
        });
    }

    $(function () {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#btnAddAutoPush').bind('click', function (e) {
            e.preventDefault();
            $('#AddAutoPushWindow').data('tWindow').center().open();
        });
        $('#notePush').bind('click', function (e) {
            e.preventDefault();
            $('#NotePushWindow').data('tWindow').center().open();
        });

        $('#checkAllRecords').click(function checkAll() { $("#AutoPushGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            });
        });
        $("#searchAutoPush").focus(function () {
            if (this.value == "定时推送名") {
                $("#searchAutoPush").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchAutoPush").val("定时推送名");
            }
        });

        $('#subEditAutoPush').click(function () {
            var len = $('#divAutoPushDetail :checked').length;
            var test = $('#divAutoPushDetail :checked');
            if (len <= 0) {
                if (confirm('你当前没有选择任何时间段，你确定要这样嘛？')) {
                    AddEditAutoPushDetail();
                }
            }
            else {
                AddEditAutoPushDetail();
            }
        });
    });

</script>
